package com.cms.service;

import java.util.List;

import com.cms.model.Category;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**   
 *   
 * @author zhanghusong   
 */
public class CategoryServiceImpl extends AbstractDateService implements CategoryService {

	@Override
	public boolean deleteCategory(Category model) {
		String sql = "delete from t_category where id="+model.getId();
		logger.info("[SQL] [method:deleteCategory] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<Category> getCategory() {
		String sql = "select * from t_category order by id";
		logger.info("[SQL] [method:getCategory] ["+sql+"]");
		ModelGenerator<Category> model = new ModelGenerator<Category>(Category.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public Category getCategoryById(Category model) {
		String sql = "select * from t_category where id="+model.getId()+" order by id";
		logger.info("[SQL] [method:getCategoryById] ["+sql+"]");
		ModelGenerator<Category> category = new ModelGenerator<Category>(Category.class);
		return category.generate(base,sql.toString());
	}

	@Override
	public boolean saveCategory(Category model) {
		model.setId(getTableId("t_category"));
		String sql = "insert into t_category (id, categoryname, parentid," +
				" createtime, createuser, remark) " +
				"values ("+model.getId()+", '"+model.getCategoryName()+
				"', '"+model.getParentId()+"', now(), '"+model.getCreateUser()+"', '"+model.getRemark()+"')";
		logger.info("[SQL] [method:saveCategory] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateCategory(Category model) {
		String sql = "update t_category set categoryname = '"+model.getCategoryName()+
		"',remark = '"+model.getRemark()+"' where id= "+model.getId();
		logger.info("[SQL] [method:updateCategory] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

}
